package com.example.texted.rsa;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.HexUtil;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.*;

public class PublicKeyTransform {

    public static void main(String[] args) throws Exception {
//	getPublicKeyTest();
//        exportPubKey();
        //exportPriKey();
        rsaSignTest();
//		Calendar calendar = new GregorianCalendar();
//		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//		Date date = new Date();
//		calendar.setTime(date); 
//	    calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动 
//	    date=calendar.getTime();   //这个时间就是日期往后推几个小时的结果 
//	    String authtime = formatter.format(date);
//    System.out.println(authtime);
        //generatePrivateKey(HexUtils.hex2bin("30820276020100300D06092A864886F70D0101010500048202603082025C02010002818100BEFB9F4B81AD110D85FE6ADB9B1E2532BBCCADF41691FCD6E633CD1982FCA1A75FB2F06613A2FD7C71D7BDB4F157D31C768E54F46E1C138CE9CAAA0022B8D6F0EB004BBA8FC831CE9610DC253D6392CEC791EC5502BF126DD4C8BCABD8424383D72BF043AAE9882089A79F01529608378890FB781A81AC280D832880D609D6DB0201030281807F526A32567360B3AEA99C9267696E21D28873F80F0BFDE49977DE1101FDC11A3FCCA0440D1753A84BE529234B8FE212F9B438A2F412B7B346871C001725E49F756BA854E1D9404E18CE1F8A3CE9FF96A61748B21B51CE4C9E12BF8A0D7D9822C347DC3BEE5438962722509F2893685D78F5805B06E535ECE256C1B3571601FB024100E95913BEFD292D23FF0F9744D0ED765DEDD978803B7B7D9528043FB6814602683778B0DCACE438B9D48DEDA27CD5BFFADD06A15B6DE12697FEB920FFEB8C631F024100D185BB7C3FD9243571CC159111171D0EE09586C99E48DF65BFA85DA642BFDCE77AC7750D1886FA857A66387018E32BB0761A19942248B4ACBB47E4F3E7DC70C50241009B90B7D4A8C61E17FF5FBA2DE09E4EE9493BA5AAD25253B8C5582A79AB84019ACFA5CB3DC89825D1385E9E6C53392AA73E046B924940C465547B6B5547B2ECBF0241008BAE7CFD7FE61823A132B90B60BA135F4063AF31143094EE7FC593C42C7FE89A51DA4E08BB04A703A6EED04ABB421D204EBC110D6C3078732785434D453DA0830241008386E46B042168F2FB4F42BEADBCBE8B1F6BE26A9B4E6D92BF98CC3B14F0173CAD756F43C47B13228F473C47FECE0E0AB5323EC4CC36F21D04E2A4D6718D7AE3"));
//	geneneratePublicKey(HexUtil.decodeHex("30818702818100B67C2D7399CBF1F3AEF3A8CD900F50E398F8984FD7B48524A727723237EBFBB42A4BF5DF4D86C81AECDB585DDE8EFBA8CE7898BA71A2626DBEFD64A4A4ECB299537F665C251B91299F0EF82DFBB00A51624994294DD692C85E858DE72B3C4B9C1366703104829C652549267D4BCAAB2E05DF885BED5C9DB6653324018A429821020103"));
    }

    /**
     * BASE64
     *
     * @throws Exception
     */
    public static void getPublicKeyTest() throws Exception {
        String e = "65537";
        //String n = "8607EFC858F019FA5C2C4531796B9D473146AAB02E428969286D3045D088EDAE493B7C0D02D3542488A074CCEA980709AF9ACE87EC0011441E942D2C619379AD042002426684D3C6C8E9AE6A4BB6F65C682C51752658B192734BD2E1DDD844B420DB20AE83A9AEA023DB046AF81E994B40671BF2372D2117CDA02780B12AB89D";
        String n = "AA2228E5F9ADDF2910DF084F4AD05F918F0482127ECF74C7063033742AFEFB5BF1EFD51E8335C9F8914770BF69A34096128B440D5AD0CEDD43796EB46FA5D9DFEF27784A748B9754C20CD151602CDFB3C6FDD24E178BC757ED6DA61D604FD3595EFB2513960103ED7B934B516B0DD753F32A6F958819A6578353DD2A358142C1";

        BigInteger bigIntE = new BigInteger(e);
        BigInteger bigPub = new BigInteger(n, 16);

        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigPub, bigIntE);
        KeyFactory pubKeyFactory = KeyFactory.getInstance("RSA");
        String publicKey = Base64.encode(pubKeyFactory.generatePublic(keySpec).getEncoded());
        System.out.println(publicKey);
    }

    public static void exportPubKey() throws Exception {
        //String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhleRa5U8utyes8QHScVSQKfxZSnm2HV6YZ+huHymBCTLg/XOZVepYM1aMoJ/EdsO/tcYxKcBCfxZ0jhQUCeZsByRvOiQzTq0XYJIvxVLG/l0pzUy13dpgBWKgNZ6N2XxJ6UTYia3bTt3jsawPcI1brclYDfs8H4/X+lo2Xe8PDPTG80bao8qEfRzfda5+1yPakGxE5qFNBB2+WDLpuElZ4nUphUfAQ7n100+1MDxGL0GNu70ccO3VXkX81N2HWXpg402gQPsAofQtRI5KtgnIwqvfaYEClRe4Sf0eYL/DsFajUF3juc/BMOl2/CGzERHDhKo9BDBDCmhX0aWIq+QuwIDAQAB";
        String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2Wft9FHJEb54M0muc6In5C8rwpe+CepJW0QKg5LEiHF8gJsJLb4BNZPN9kDXctxtlRzrC/+i5prgttdq6otYBAiBqThQeaTkSCMIBx3tlq08r8Z6roSDrg7vs8Y1ivTrw2DHWXhoIIB6b/N6MnnI010YqPOkTIt4j0FH5UnJp/LQOYhAqxpMajlE3In/mXs1I7xhydo3PCMvgYS5+yCR76qpxw5+jyLG58LxIZmuui6TxrBYCXJ+JWAnCPF/aGctZ+Z5P7aJfAQezeEnXwGOLBjllEztByMyWj1WV0NPyRzeXZxgjnsJpkCTDCpBv5e5r/UJvG0WDGBYbYM0xtIiewIDAQAB";
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] encodedKey = Base64.decode(publicKey);
        RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
        System.out.println(pubKey);
        byte[] modulesBytes = new byte[128];

        byte[] pubKeyBytes = pubKey.getModulus().toByteArray();
        System.arraycopy(pubKeyBytes, pubKeyBytes.length - 128, modulesBytes, 0, 128);
        String pubKeyHex = HexUtil.encodeHexStr(modulesBytes);
        BigInteger e = pubKey.getPublicExponent();
        System.out.println(pubKeyHex);

        byte[] pubKeyBytes2 = pubKey.getModulus().toByteArray();
        System.arraycopy(pubKeyBytes, pubKeyBytes2.length - 128, modulesBytes, 0, 128);
        String pubKeyHex2 = HexUtil.encodeHexStr(modulesBytes);
        BigInteger e1 = pubKey.getPublicExponent();
        System.out.println(pubKeyHex2);

    }

    public static void exportPriKey() throws Exception {
        String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALv+AL0t3jkWtPo87nzpSvzS+NqCM76bTlRjkJJVC68UquCb/CszOk2bB+IciiMf7DqI12C58GTi7OmzAEEyzVAWXZtvp0383GRkaSvEfFaL/Q38NiMYlYBI1IU7oqT+hBUNN24B8p688JjSdcR/rkT/AauzBDzRe1SUUbQhW4QXAgMBAAECgYB9a0F89Pv8JF0LUdleNpqTwADMbu8srBp/DInoRprr7KLwBMEg5SVxnwuMW7QBKQwGJUNjwclS1V5vr79swALe0mhh15Q9H8J5AFd1zkjcHEFeWr7ql7cdAlaGt8WQcZMHBUThA8NFCiYIySWq7YySOw3FSy3fdBPsh4S85M8/qQJBAP8+Ri6CUqEHxIbsGwhOzGSpOpMXZsns8ZgFSX6+sJ6ImhO1dIUOt3/m3nMIKAoAklMxOSjex+hqwLHu6bsiXGMCQQC8jK+pkxNyRPpwG5h61+tIQN6WVMOQVg47+BDrDkkIPGXIk+UqxwiBG7iP/7qqIYMPZZRD1Qcy7ITOFDypwSW9AkACHmI2wJ4u+AGJnSwcRYssyrrRvb/8rRrUSDbz9mFA76FXyInPEPA0/5WZHtg2PQr55c6zqCXAqCIxV+pYw1BBAkByfiudGUZRuTkqIlexILQqp9mCB+7JEriKCdScJ4yJh8N2zPf3xeyQGbk1F+DNtpPTIaeqmQ1KCv2J9hfwLO0xAkEAl4XaOv+pnTIxLQErAuIH8LLl+D+SCxCyoBJyWcRAjVSzKZY0o/tcZ5On9Wojb+EMFEBUwGmKbfI7qoI/yUpRPg==";
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] encodedKey = Base64.decode(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
        BigInteger d = priKey.getPrivateExponent();
        System.out.println(HexUtil.encodeHexStr(d.toByteArray()));
    }

    public static void rsaSignTest() throws Exception {
        String e = "65537";
        String n = "AA2228E5F9ADDF2910DF084F4AD05F918F0482127ECF74C7063033742AFEFB5BF1EFD51E8335C9F8914770BF69A34096128B440D5AD0CEDD43796EB46FA5D9DFEF27784A748B9754C20CD151602CDFB3C6FDD24E178BC757ED6DA61D604FD3595EFB2513960103ED7B934B516B0DD753F32A6F958819A6578353DD2A358142C1";
        String d = "8B56FCDE1596A4BDF276684CE0BE36777585C0AE3B33D7B5A548D6EB5EC784C0722EF6024E7E40539750C673CEA60920A0417553E6BC3BA096419F3CA2705836ABB2B0C617FCF2C6C8A0FF490D7C484A23E2C70E69BFD70358D10B284C3AC893CCBFC3CC625C579B6E471196EC924CD049E9F8A45FAC32D1DFC6B1D7AE9D9C91";
        BigInteger bigIntE = new BigInteger(e);
        BigInteger bigPub = new BigInteger(n, 16);
        BigInteger bigPri = new BigInteger(d, 16);

        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigPub, bigIntE);
        KeyFactory pubKeyFactory = KeyFactory.getInstance("RSA");
        String publicKey = Base64.encode(pubKeyFactory.generatePublic(keySpec).getEncoded());
        System.out.println(publicKey);

        RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(bigPub, bigPri);
        //PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory priKeyFactory = KeyFactory.getInstance("RSA");
        String privateKey = Base64.encode(priKeyFactory.generatePrivate(priKeySpec).getEncoded());
        System.out.println(privateKey);
    }


    public static void generatePrivateKey(byte[] key) throws NoSuchAlgorithmException, InvalidKeySpecException {
        KeySpec keySpec = new PKCS8EncodedKeySpec(key);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPrivateKey priKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
        BigInteger d = priKey.getPrivateExponent();
        System.out.println(HexUtil.encodeHexStr(d.toByteArray()));
    }

    public static void geneneratePublicKey(byte[] key) throws InvalidKeySpecException, NoSuchAlgorithmException {
//	        KeySpec keySpec = new X509EncodedKeySpec(key);  
//	        KeyFactory keyFactory = KeyFactory.getInstance("RSA");  
//	        RSAPublicKey  pubKey =  (RSAPublicKey) keyFactory.generatePublic(keySpec);  
//	        
//	        byte[] modulesBytes = new byte[128];
//	        byte[] pubKeyBytes = pubKey.getModulus().toByteArray();
//	        System.arraycopy(pubKeyBytes, pubKeyBytes.length - 128, modulesBytes, 0, 128);
//	        String pubKeyHex = HexUtils.bin2hex(modulesBytes);
//	        BigInteger e = pubKey.getPublicExponent();
//	        System.out.println(pubKeyHex);
        KeySpec keySpec = new X509EncodedKeySpec(key);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        System.out.println(keyFactory.generatePublic(keySpec));
    }


}
